<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>

<body>
  <h1>响应拦截器</h1>
  <button id="btn">点击获取数据</button>
  <script>
    // 获取元素
    const obtn = document.getElementById('btn')

    // 创建axios实例
    const myAxios = axios.create({
      baseURL: '/',
      timeout: 1000,
      headers: {}
    })

    // 设置响应拦截器
    myAxios.interceptors.response.use(function (response) {
      console.log(response);
      return response.data.data
    }, function (err) {
      return Promise.reject(err)
    })

    obtn.onclick = async function () {
      try {
        const re = await myAxios.get('/user', {
        params: {
          userId: '001'
        }
      })
      console.log(re);
      }catch(e){
        console.log(e);
      }
    }
  </script>
</body>

</html>